﻿using ExpenseSystem.Common;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Fether;
using ExpenseSystem.Lor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ExpenseSystem
{
    public static class SystemControllerHelper
    {
        public static ResponseModel List<T>(SystemFetcher<T> fetcher,string authKey,Restrain[] restrains,Inhibition inhibition) where T:SystemModel,new()
        {
            if (restrains == null)
                restrains = new Restrain[0];
            ResponseModel responseModel = new ResponseModel();
            Authority authority = new Authority();
            if (!String.IsNullOrEmpty(authKey))
            {
                if (!authority.GetAuthority(authKey))
                {
                    responseModel.Result = Definition.RESP_NOT_AUTH;
                    responseModel.Count = 0;
                    responseModel.Data = null;
                    responseModel.Message = "没有授予对应的权限";
                    return responseModel;
                }
            }
            string message = "";
            int rows;
            IList<T> models = fetcher.List( OrderBy.GetDefault(), Limit.GetDefault(), out message,out rows, restrains);
            responseModel.Count = rows;
            responseModel.Result = Definition.RESP_SUCCESS;
            responseModel.Message = message;
            responseModel.Data = JsonHelper.ArrayToJson(models, inhibition);
            return responseModel;
        }
        public static ResponseModel Get<T>(SystemFetcher<T> fetcher, string authKey, long id, Inhibition inhibition) where T : SystemModel, new()
        {
            ResponseModel responseModel = new ResponseModel();
            Authority authority = new Authority();
            if (!String.IsNullOrEmpty(authKey))
            {
                if (!authority.GetAuthority(authKey))
                {
                    responseModel.Result = Definition.RESP_NOT_AUTH;
                    responseModel.Count = 0;
                    responseModel.Data = null;
                    responseModel.Message = "没有授予对应的权限";
                    return responseModel;
                }
            }
            string message = "";
            T model = fetcher.Get( id, out message);
            if (model == null)
            {
                responseModel.Count = 0;
                responseModel.Result = Definition.RESP_DATA_NOT_FOUND;
                responseModel.Message = message;
                responseModel.Data = JsonHelper.ToJson(model, inhibition);

            }
            else
            {
                responseModel.Count = 1;
                responseModel.Result = Definition.RESP_SUCCESS;
                responseModel.Message = message;
                responseModel.Data = JsonHelper.ToJson(model, inhibition);

            }
            return responseModel;
        }

        public static ResponseModel Set<T>(SystemFetcher<T> fetcher, string authKey, T data,ISystemManager<T> manager, Inhibition inhibition) where T : SystemModel, new()
        {
            UserProfile userProfile = Authority.GetUserProfile();
            ResponseModel response = new ResponseModel();
            Authority authority = new Authority();
            if (!authority.GetAuthority(authKey))
            {
                response.Result = Definition.RESP_NOT_AUTH;
                response.Count = 0;
                response.Data = null;
                response.Message = "没有授予对应的权限";
                return response;
            }
            MergeHelper mergeHelper = new MergeHelper(inhibition);
            T model = mergeHelper.SystemObjMerge(data, fetcher);
            string message = "";
            bool result = manager.Save(model,  out message);
            if (!result) SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);
            if (result)
            {
                response.Result = Definition.RESP_SUCCESS;
                response.Count = 1;
                response.Data = JsonHelper.ToJson(model, inhibition);
            }
            else
            {
                response.Result = Definition.RESP_SET_DATA_ERROR;
                response.Count = 0;
                response.Data = response.Message = message;
                SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);

            }
            return response;
        }

        public static ResponseModel Remove<T>(string authKey, T data, ISystemManager<T> manager) where T : SystemModel, new()
        {
            ResponseModel response = new ResponseModel();
            Authority authority = new Authority();
            if (!authority.GetAuthority(authKey))
            {
                response.Result = Definition.RESP_NOT_AUTH;
                response.Count = 0;
                response.Data = null;
                response.Message = "没有授予对应的权限";
                return response;
            }
            string message = "";
            bool result = manager.Delete(data,  out message);
            if(!result)SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);
            if (result)
            {
                response.Result = Definition.RESP_SUCCESS;
                response.Count = 1;
                response.Message = message;
            }
            else
            {
                response.Result = Definition.RESP_SET_DATA_ERROR;
                response.Count = 0;
                response.Data = response.Message = message;
                SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);

            }
            return response;
        }
        public static ResponseModel RemoveById<T>(string authKey,long[] id, ISystemManager<T> manager) where T : SystemModel, new()
        {
            UserProfile userProfile = Authority.GetUserProfile();
            ResponseModel responseModel = new ResponseModel();
            Authority authority = new Authority();
            if (!authority.GetAuthority(authKey))
            {
                responseModel.Result = Definition.RESP_NOT_AUTH;
                responseModel.Count = 0;
                responseModel.Data = null;
                responseModel.Message = "没有授予对应的权限";
                return responseModel;
            }
            string message = "";
            bool result = manager.DeleteById(id,  out message);
            if (!result) SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);
            if (result)
            {
                responseModel.Result = Definition.RESP_SUCCESS;
                responseModel.Count = 1;
                responseModel.Message = message;
            }
            else
            {
                responseModel.Result = Definition.RESP_SET_DATA_ERROR;
                responseModel.Count = 0;
                responseModel.Data = responseModel.Message = message;
                SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);

            }
            return responseModel;
        }
        public static ResponseModel ListDetail<T>(IDetailSystemFetcher<T> fetcher,string authKey,long masterId,Inhibition inhibition)where T:SystemModel,new()
        {
            ResponseModel responseModel = new ResponseModel();
            Authority authority = new Authority();
            if (!authority.GetAuthority(authKey))
            {
                responseModel.Result = Definition.RESP_NOT_AUTH;
                responseModel.Count = 0;
                responseModel.Data = null;
                responseModel.Message = "没有授予对应的权限";
                return responseModel;
            }
            string message = "";
            int rows;
            IList<T> model = fetcher.ListByMainId( masterId, out message,out rows);
            responseModel.Count = rows;
            responseModel.Result = Definition.RESP_SUCCESS;
            responseModel.Message = message;
            responseModel.Data = JsonHelper.ArrayToJson(model, inhibition);
            return responseModel;

        }
        public static ResponseModel SetDetail<T>(IDetailSystemFetcher<T> dfetcher, SystemFetcher<T> fetcher,string authKey,RequestModel<T> requestData,ISystemManager<T> manager,Inhibition inhibition) where T : SystemModel, new()
        {
            ResponseModel response = new ResponseModel();
            Authority authority = new Authority();
            if (!authority.GetAuthority(authKey))
            {
                response.Result = Definition.RESP_NOT_AUTH;
                response.Count = 0;
                response.Data = null;
                response.Message = "没有授予对应的权限";
                return response;
            }
            MergeHelper mergeHelper = new MergeHelper(inhibition);
            T[] models = new T[requestData.Data.Count()];
            string message = "";
            int rows;
            IList<T> modelsInDB = dfetcher.ListByMainId( requestData.MasterId, out message,out rows);
            MyEqualityComparer<T> equalityComparer = new MyEqualityComparer<T>();
            IEnumerable<T> data4Delete = modelsInDB.Except(modelsInDB.Intersect(models, equalityComparer), equalityComparer);
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                T m = mergeHelper.SystemObjMerge(requestData.Data[i], fetcher);
                models[i] = m;
            }
            bool successed = true;
            foreach (T ru in data4Delete)
            {
                successed = manager.Delete(ru,  out message);
                if (!successed)
                    break;
            }
            if (successed)
            {
                foreach (T ru in models)
                {
                    successed = manager.Save(ru,  out message);
                    if (!successed)
                        break;
                }
            }
            if (!successed) SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);
            if (successed)
            {
                response.Result = Definition.RESP_SUCCESS;
                response.Count = 1;
                response.Data = JsonHelper.ArrayToJson(models, inhibition);
            }
            else
            {
                response.Result = Definition.RESP_SET_DATA_ERROR;
                response.Count = 0;
                response.Data = response.Message = message;
                SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);

            }
            return response;
        }
    }
}